from qfluentwidgets import (
    NavigationItemPosition,
    FluentWindow,
    SubtitleLabel,
    setFont,
    SplashScreen,
    SystemThemeListener,
)
from qfluentwidgets import FluentIcon as FIF
from PyQt6.QtWidgets import (
    QApplication,
    QLabel,
    QVBoxLayout,
    QPushButton,
    QHBoxLayout,
    QFrame,
)
from PyQt6.QtGui import QIcon
from PyQt6.QtCore import QModelIndex, Qt, QRect, QSize

from views.all_stock_list_view import AllStockListInterface
from views.common.icon import Icon
from views.common_setting_view import CommonSettingView
from views.home_view import HomeView
from views.stock_day_view import StockDayView
from views.base_gallery_view import GalleryInterface
from views.stock_monitor_view import StockMonitorView
from views.demo_chat_view import DemoStockChatView
from views.strategy_group_view import StrategyGroupView
from views.template_chat_view import BaseInfoTemplateView
from views.stock_real_time_view import StockRealTimeView
from views.stock_risk_view import StockRiskView
from views.stock_recommend_view import StockRecommend


from views.common.signal_bus import signalBus
from views.common.translator import Translator
from views.common.config import ZH_SUPPORT_URL, EN_SUPPORT_URL, cfg


class Widget(QFrame):

    def __init__(self, text: str, parent=None):
        super().__init__(parent=parent)
        self.label = SubtitleLabel(text, self)
        self.hBoxLayout = QHBoxLayout(self)

        setFont(self.label, 24)
        self.hBoxLayout.addWidget(self.label, 1)

        # 必须给子界面设置全局唯一的对象名
        self.setObjectName(text.replace(" ", "-"))


class MainWindow(FluentWindow):
    def __init__(self):
        super().__init__()
        self.initWindow()

        # create system theme listener
        self.themeListener = SystemThemeListener(self)

        self.homeInterface = HomeView(self)
        self.stockRecommendView = StockRecommend(self)
        self.firstView = Widget("大盘预警", self)
        self.listInterface = AllStockListInterface(self)
        self.stockDayInterface = StockDayView(self)
        self.stockMonitorView = StockMonitorView(self)
        self.demoStockChatView = DemoStockChatView(self)
        self.baseTemplateView = BaseInfoTemplateView("demoTemplate", self)
        self.stockRealTimeView = StockRealTimeView(self)
        self.stockRiskView = StockRiskView(self)
        self.commonSetting = CommonSettingView(self)
        self.strategyGroupView = StrategyGroupView(self)

        # enable acrylic effect
        self.navigationInterface.setAcrylicEnabled(True)
        self.connectSignalToSlot()

        self.initNavigation()

        self.splashScreen.finish()

        # start theme listener
        self.themeListener.start()

    def initNavigation(self):
        t = Translator()
        pos = NavigationItemPosition.SCROLL

        self.addSubInterface(self.homeInterface, FIF.HOME, "首页", pos)
        self.addSubInterface(
            self.stockRecommendView, FIF.EXPRESSIVE_INPUT_ENTRY, "个股推荐", pos
        )
        self.addSubInterface(self.listInterface, FIF.LIBRARY, "股票列表", pos)
        self.addSubInterface(self.stockDayInterface, FIF.MARKET, "个股走势", pos)
        self.navigationInterface.addSeparator()
        self.addSubInterface(self.demoStockChatView, FIF.QUESTION, "报表案例", pos)
        # self.addSubInterface(self.stockMonitorView, FIF.HOME, "预警信息", pos)
        self.addSubInterface(self.strategyGroupView, FIF.HOME, "策略报表", pos)
        # self.addSubInterface(self.stockRealTimeView, FIF.QUESTION, "实时价格", pos)
        # self.addSubInterface(self.stockRiskView, FIF.RINGER, "功能报警", pos)

        self.addSubInterface(
            self.commonSetting,
            FIF.SETTING,
            self.tr("Settings"),
            NavigationItemPosition.BOTTOM,
        )

    def initWindow(self):
        self.resize(1200, 1000)
        self.setMinimumWidth(1000)
        self.setWindowIcon(QIcon(":/gallery/images/logo.png"))
        self.setWindowTitle("PyQt-Fluent-Widgets")

        self.setMicaEffectEnabled(cfg.get(cfg.micaEnabled))

        # create splash screen
        self.splashScreen = SplashScreen(self.windowIcon(), self)
        self.splashScreen.setIconSize(QSize(106, 106))
        self.splashScreen.raise_()

        desktop = QApplication.screens()[0].availableGeometry()
        w, h = desktop.width(), desktop.height()
        self.move(w // 2 - self.width() // 2, h // 2 - self.height() // 2)
        self.show()
        QApplication.processEvents()

    def resizeEvent(self, e):
        super().resizeEvent(e)
        if hasattr(self, "splashScreen"):
            self.splashScreen.resize(self.size())

    def closeEvent(self, e):
        self.themeListener.terminate()
        self.themeListener.deleteLater()
        super().closeEvent(e)

    def connectSignalToSlot(self):
        signalBus.micaEnableChanged.connect(self.setMicaEffectEnabled)
        signalBus.switchToSampleCard.connect(self.switchToSample)

    def switchToSample(self, routeKey, index, data):
        """switch to sample"""
        interfaces = self.findChildren(GalleryInterface)
        for w in interfaces:
            if w.objectName() == routeKey:
                w.setExchangeData(data)
                self.stackedWidget.setCurrentWidget(w, False)
                w.scrollToCard(index)
